package current.security.user.manager.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import office.action.system.vo.DepartmentVo;
import office.bo.system.Department;
import office.service.system.ISystemService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import platform.common.action.BaseAction;
import current.security.user.domain.R_Resc;
import current.security.user.domain.Resc;
import current.security.user.domain.Role;
import current.security.user.domain.U_Role;
import current.security.user.domain.User;
import current.security.user.manager.service.UserManagerService;

@Namespace("/userManager")
@ParentPackage("struts-base")
@Controller("userManagerAction")
@Scope("prototype")
public class UserManagerAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	@Resource
	private UserManagerService userManagerService;
	private ISystemService systemService;
	private List<User> userList = new ArrayList<User>();
	private List<Resc> rescList = new ArrayList<Resc>();
	private List<Role> roleList = new ArrayList<Role>();
	private List<R_Resc> r_rescList = new ArrayList<R_Resc>();
	private List<R_Resc> r_rescHaveList = new ArrayList<R_Resc>();
	private Map<String,Resc> r_rescHaveMap = new HashMap<String,Resc>();
	private List<Resc> r_rescNoHaveList = new ArrayList<Resc>();
	private List<U_Role> u_roleList = new ArrayList<U_Role>();
	private List<DepartmentVo> departmentList = new ArrayList<DepartmentVo>();
	private User user;
	private Resc resc;
	private Role role;
	private List<String> rescIds = new ArrayList<String>();
	
	@Action(value="saveR_Rescs",results={@Result(type="redirect",location="/userManager/getAllRole.action")})
	public String saveR_Rescs(){
		userManagerService.deleteR_Resc(role.getId());
		for (Iterator<String> iterator = rescIds.iterator(); iterator.hasNext();) {
			String type = (String) iterator.next();
			if(type != null){
				R_Resc r_resc = new R_Resc();
				Role srole = new Role();
				srole.setId(role.getId());
				Resc resc = new Resc();
				resc.setId(type);
				r_resc.setResc(resc);
				r_resc.setRole(srole);
				userManagerService.addObject(r_resc);
			}
		}
		return SUCCESS;
	}
	
	@Action(value="getAllResc",results={@Result(location="/userManager/rescContent.jsp")})
	public String getAllResc(){
		rescList = userManagerService.getAllResc();
		return SUCCESS;
	}
	
	@Action(value="getAllRescForRole",results={@Result(location="/userManager/chooseResc.jsp")})
	public String getAllRescForRole(){
		rescList = userManagerService.getRescByType();
		if(rescList != null){
			Role tempRole = (Role)userManagerService.getObjectById(Role.class, role.getId());
			for (Iterator<Resc> iterator = rescList.iterator(); iterator.hasNext();) {
				Resc resc = (Resc) iterator.next();
				if(tempRole != null && tempRole.getRescs() != null){
					for (Iterator<R_Resc> iteratorRR = tempRole.getRescs().iterator(); iteratorRR.hasNext();) {
						R_Resc type = (R_Resc) iteratorRR.next();
						if(resc.getId() != null && resc.getId().equals(type.getResc().getId())){
							r_rescHaveMap.put(resc.getId(), resc);
							break;
						}
					}
				}
			}
		}
		return SUCCESS;
	}
	
	@Action(value="addResc",results={@Result(type="redirect",location="/userManager/getAllResc.action")})
	public String addResc(){
		if(resc != null && resc.getId() != null && resc.getId().equals("")){
			resc.setId(null);
		}
		userManagerService.addObject(resc);
		this.getAjaxMessagesJson().setMessage(true, "", "保存成功");
		return RESULT_AJAXJSON;
	}
	
	@Action(value="addRole",results={@Result(type="redirect",location="/userManager/getAllRole.action")})
	public String addRole(){
		if(role != null && role.getId() != null && role.getId().equals("")){
			role.setId(null);
		}
		this.getAjaxMessagesJson().setMessage(true, "", "保存成功");
		userManagerService.addObject(role);
		return RESULT_AJAXJSON;
	}
	
	@Action(value="initAddResc",results={@Result(location="/userManager/rescAdd.jsp")})
	public String initAddResc(){
		if(resc != null){
			resc = (Resc)userManagerService.getObjectById(Resc.class, resc.getId());
		}
		return SUCCESS;
	}
	
	@Action(value="initAddRole",results={@Result(location="/userManager/roleAdd.jsp")})
	public String initAddRole(){
		if(role != null){
			role = (Role)userManagerService.getObjectById(Role.class, role.getId());
		}
		getAllResc();
		return SUCCESS;
	}
	
	@Action(value="initAddUser",results={@Result(location="/userManager/userAdd.jsp")})
	public String initAddUser(){
		if(user != null){
			user = (User)userManagerService.getObjectById(User.class, user.getId());
		}
		getAllRole();
		List<Department> list = systemService.getAllDepartment();
		for (Iterator<Department> iterator = list.iterator(); iterator.hasNext();) {
			Department department = (Department) iterator.next();
			DepartmentVo departmentVo = new DepartmentVo();
			departmentVo.setId(department.getId().toString());
			departmentVo.setName(department.getName());
			departmentVo.setMemo(department.getMemo());
			departmentVo.setCreateDate(department.getCreateDate());
			departmentVo.setType(department.getType());
			departmentVo.setCompanyId(department.getCompanyId());
			departmentVo.setCompanyName(departmentVo.getCompanyName());
			departmentVo.setLeftValue(department.getLeftValue());
			departmentVo.setRightValue(department.getRightValue());
			departmentVo.setTreeLevel(department.getTreeLevel());
			departmentList.add(departmentVo);
		}
		return SUCCESS;
	}
	
	@Action(value="addUser",results={@Result(type="redirect",location="/userManager/getAllUser.action")})
	public String addUser(){
		try{
			if(user != null && user.getId() != null && user.getId().equals("")){
				user.setId(null);
				User dbuser = userManagerService.getUserByName(user.getName());
				if(dbuser != null){
					this.getAjaxMessagesJson().setMessage(false, "", "此用户名称已经被占用，请更换用户名！！");
					return RESULT_AJAXJSON;
				}
			}
			userManagerService.deleteU_Role(user.getId());
			Set<U_Role> set = new HashSet<U_Role>(u_roleList);
			for (Iterator<U_Role> iterator = set.iterator(); iterator.hasNext();) {
				U_Role thisrole = (U_Role) iterator.next();
				if(thisrole != null){
					thisrole.setUser(user);
					user.getRoles().add(thisrole);
				}
			}
			//密码进行加密
			user.getDepartment().setId(user.getDepartment().getOoid());
			if(user != null && user.getId() != null && user.getId().equals("")){
				User userTemp = (User)userManagerService.getObjectById(User.class, user.getId());
				BeanUtils.copyProperties(userTemp, user);
				userManagerService.addObject(userTemp);
			}else{
//				String spassword = MD5.md5s(user.getName(), user.getName());
				user.setPassword(user.getName());
				userManagerService.addObject(user);
			}
			this.getAjaxMessagesJson().setMessage(true, "", "保存成功");
		}catch(Exception e){
			e.printStackTrace();
		}
		return RESULT_AJAXJSON;
	}
	
	public String updateUser(){
		return null;
	}
	@Action(value="updateResc",results={@Result(location="/userManager/rescAdd.jsp")})
	public String updateResc(){
		resc = (Resc)userManagerService.getObjectById(Resc.class, resc.getId());
		return SUCCESS;
	}
	
	@Action(value="updateRole",results={@Result(location="/userManager/roleAdd.jsp")})
	public String updateRole(){
		role = (Role)userManagerService.getObjectById(Role.class, role.getId());
		getAllResc();
		return SUCCESS;
	}
	
	@Action(value="deleteUser",results={@Result(type="redirect",location="/userManager/getAllUser.action")})
	public String deleteUser(){
		userManagerService.deleteUser(user.getId());
		return SUCCESS;
	}
	
	@Action(value="stopUser",results={@Result(type="redirect",location="/userManager/getAllUser.action")})
	public String stopUser(){
		User userTemp = (User)userManagerService.getObjectById(User.class, user.getId());
		userTemp.setStatus(user.getStatus());
		return SUCCESS;
	}
	
	@Action(value="resetPassword",results={@Result(type="redirect",location="/userManager/getAllUser.action")})
	public String resetPassword(){
		User userTemp = (User)userManagerService.getObjectById(User.class, user.getId());
		userTemp.setPassword(userTemp.getName());
		return SUCCESS;
	}
	
	@Action(value="deleteRole",results={@Result(type="redirect",location="/userManager/getAllRole.action")})
	public String deleteRole(){
		userManagerService.deleteRole(role.getId());
		return SUCCESS;
	}
	
	@Action(value="deleteResc",results={@Result(type="redirect",location="/userManager/getAllResc.action")})
	public String deleteResc(){
		userManagerService.deleteResc(resc.getId());
		return SUCCESS;
	}
	
	public String getUserById(){
		return null;
	}
	@Action(value="getAllUser",results={@Result(location="/userManager/userContent.jsp")})
	public String getAllUser(){
		userList = userManagerService.getAllUser();
		return SUCCESS;
	}
	@Action(value="getAllRole",results={@Result(location="/userManager/roleContent.jsp")})
	public String getAllRole(){
		roleList = userManagerService.getAllRole();
		return SUCCESS;
	}

	/**
	 * @return the userManagerService
	 */
	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	/**
	 * @param userManagerService the userManagerService to set
	 */
	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the userList
	 */
	public List<User> getUserList() {
		return userList;
	}

	/**
	 * @param userList the userList to set
	 */
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	/**
	 * @return the rescList
	 */
	public List<Resc> getRescList() {
		return rescList;
	}

	/**
	 * @param rescList the rescList to set
	 */
	public void setRescList(List<Resc> rescList) {
		this.rescList = rescList;
	}

	public Resc getResc() {
		return resc;
	}

	public void setResc(Resc resc) {
		this.resc = resc;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<R_Resc> getR_rescList() {
		return r_rescList;
	}

	public void setR_rescList(List<R_Resc> rRescList) {
		r_rescList = rRescList;
	}

	public List<U_Role> getU_roleList() {
		return u_roleList;
	}

	public void setU_roleList(List<U_Role> uRoleList) {
		u_roleList = uRoleList;
	}

	

	public List<DepartmentVo> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<DepartmentVo> departmentList) {
		this.departmentList = departmentList;
	}

	public List<String> getRescIds() {
		return rescIds;
	}

	public void setRescIds(List<String> rescIds) {
		this.rescIds = rescIds;
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	public List<R_Resc> getR_rescHaveList() {
		return r_rescHaveList;
	}

	public void setR_rescHaveList(List<R_Resc> rRescHaveList) {
		r_rescHaveList = rRescHaveList;
	}

	public List<Resc> getR_rescNoHaveList() {
		return r_rescNoHaveList;
	}

	public void setR_rescNoHaveList(List<Resc> rRescNoHaveList) {
		r_rescNoHaveList = rRescNoHaveList;
	}

	public Map<String, Resc> getR_rescHaveMap() {
		return r_rescHaveMap;
	}

	public void setR_rescHaveMap(Map<String, Resc> rRescHaveMap) {
		r_rescHaveMap = rRescHaveMap;
	}

}
